package com.epro.dx.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.listenner.MyAsyncTaskListenner;
import com.epro.dx.task.FeedBackTask;
import com.epro.dx.util.ActivityUtil;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.weipu.common.DXApplication;
import com.weipu.common.DXApplication.TrackerName;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.model.FeedBackModel;
import com.weipu.common.util.AlertToast;
import com.weipu.common.util.StringUtil;

/**
 * FeedBack
 *
 * @author hjd008
 * @version [版本号, 2015-2-10]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class FeedBackActivity extends BaseFragmentActivity {

    EditText editContent;

    EditText editContact;

    TextView tvInputNumber;

    View tvSubmit;

//    private FeedbackAgent agent;

//    private Conversation mComversation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Tracker tracker = DXApplication.getContext().getTracker(TrackerName.APP_TRACKER);
        tracker.setScreenName(FeedBackActivity.class.getSimpleName());
        tracker.send(new HitBuilders.ScreenViewBuilder().build());
        //设置窗体无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_feedback);

        setTitleContent(R.string.aboutus_feedback);
        setLeftBtnContent(true, -1);
        tvSubmit = setRightBtnContent(R.string.user_register_title_submit);

        editContent = (EditText) findViewById(R.id.content_edit_text);
        editContact = (EditText) findViewById(R.id.contact_edit_text);
        tvInputNumber = (TextView) findViewById(R.id.tv_input_number);

        if (ActivityUtil.isLogin(this)) {
            // 进行获取email
            String email = StringUtil.getConfig(this, ServerConstant.Parameter.PARAMETER_NAME_USER_EMAIL);
            if (!TextUtils.isEmpty(email))
                editContact.setText(email);
        }

//        agent = new FeedbackAgent(this);
//        mComversation = agent.getDefaultConversation();

        tvSubmit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //校验内容
                //校验联系方式
                String content = editContent.getText().toString().trim();
                String contact = editContact.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    editContent.startAnimation(AnimationUtils.loadAnimation(FeedBackActivity.this, R.anim.shake));
                    return;
                }
                if (TextUtils.isEmpty(contact)) {
                    editContact.startAnimation(AnimationUtils.loadAnimation(FeedBackActivity.this, R.anim.shake));
                    return;
                }

                if (!StringUtil.isEmail(contact)) {
                    AlertToast.alert(FeedBackActivity.this, R.string.user_login_validate_account_rule, editContact);
                    return;
                }
                // 将内容添加到会话列表
//                mComversation.addUserReply(content);

                new FeedBackTask(contact, content, new MyAsyncTaskListenner() {
                    @Override
                    public void onPreTask() {

                    }

                    @Override
                    public void onPostTask(String result, Object obj) {
                        if (obj != null && obj instanceof FeedBackModel) {
                            if (((FeedBackModel) obj).Success) {
                                AlertToast.alert(FeedBackActivity.this, R.string.submit_success);
                            }
                        }

                    }
                }).execute();

                editContent.getEditableText().clear();
                editContact.getEditableText().clear();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(editContact.getWindowToken(), 0);
                }
                finish();
            }
        });

        editContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
                String str = editable.toString();
                if (!TextUtils.isEmpty(str)) {
                    tvInputNumber.setText(str.length() + "/200");
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

        });

    }

}
